package com.cqh.recruit.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqh.recruit.mapper.ActivityCheckMapper;
import com.cqh.recruit.mapper.ActivityMapper;
import com.cqh.recruit.mapper.FeedbackMapper;
import com.cqh.recruit.pojo.dto.FeedbackDTO;
import com.cqh.recruit.pojo.dto.MFeedQueryDto;
import com.cqh.recruit.pojo.entity.Activity;
import com.cqh.recruit.pojo.entity.ActivityCheck;
import com.cqh.recruit.pojo.entity.Feedback;
import com.cqh.recruit.pojo.vo.MFeedListVo;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 学生反馈信息表 服务实现类
 * </p>
 *
 * @author dh
 * @since 2025-10-18
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityCheckMapper activityCheckMapper;

    @Autowired
    private FeedbackMapper feedbackMapper;
    /**
     * 提交反馈
     */
    @Override
    public Result submitFeedback(Long studentId, FeedbackDTO dto) {
        // 1. 验证活动是否存在
        Activity activity = activityMapper.selectById(dto.getActivityId());
        if (activity == null) {
            return Result.buildFail(20001,"活动不存在");
        }

        // 2. 验证学生是否报名且通过审核
        ActivityCheck activityCheck = activityCheckMapper.selectByActivityAndStudent(dto.getActivityId(), studentId);
        if (activityCheck == null) {
            return Result.buildFail(20001,"您未报名该活动");
        }
        if (activityCheck.getStatus() != 1) {
            return Result.buildFail(20001,"您的报名未通过审核，无法提交反馈");
        }

        // 3. 验证活动状态（进行中或已结束）
        if (activity.getStatus() < 3) {
            return Result.buildFail(20001,"活动尚未开始，暂不能提交反馈");
        }

        // 4. 检查是否已提交过反馈
        Feedback existingFeedback = findByStudentAndActivity(studentId, dto.getActivityId());
        if (existingFeedback != null) {
            return Result.buildFail(20001,"您已经提交过反馈");
        }

        // 5. 保存反馈
        Feedback feedback = new Feedback();
        feedback.setStudentId(studentId);
        feedback.setActivityId(dto.getActivityId());
        feedback.setFeedbackInfo(dto.getFeedbackInfo());
        feedback.setEmployeeId(0L);// 暂不处理员工信息, 默认为0
        feedback.setCreateTime(LocalDateTime.now());

        this.save(feedback);
        return Result.buildSuccess("反馈提交成功");
    }

    /**
     * 查询当前学生对某活动的反馈
     *
     * @param studentId 学生ID
     * @param activityId 活动ID
     * @return 学生对指定活动的反馈信息，如果不存在则返回null
     */
    @Override
    public Feedback findByStudentAndActivity(Long studentId, Long activityId) {
        // 创建查询条件
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Feedback::getStudentId, studentId)
                .eq(Feedback::getActivityId, activityId);
        return this.getOne(queryWrapper);
    }
    @Override
    public Page<MFeedListVo> findFeedByQueryDto(MFeedQueryDto feedQueryDto) {
        Page page=new Page(feedQueryDto.getPage(),feedQueryDto.getLimit());
        return getBaseMapper().findFeedByQueryDto(page,feedQueryDto);
    }

    @Override
    public MFeedListVo findFeedById(Long id) {
        return feedbackMapper.findFeedById(id);
    }


}
